package fr.jade.reflex.factory;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Type;
import org.objectweb.fractal.api.control.IllegalContentException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.factory.InstantiationException;

import fr.jade.fraclite.InitializationContext;
import fr.jade.fraclite.factory.BasicGenericFactory;
import fr.jade.reflex.api.control.ContentNotificationController;

public class CompositeGenericFactory extends BasicGenericFactory {

  ContentNotificationController cc = null;

  public void initFcController(final InitializationContext ic) throws InstantiationException {
    super.initFcController(ic);
    cc = (ContentNotificationController) ic.getInterface("content-controller");
  }

  public Component newFcInstance(final Type type, final Object controllerDesc, final Object contentDesc) throws InstantiationException {

    Component cmp = super.newFcInstance(type, controllerDesc, contentDesc);
    // add the new component as a sub-component
    try {
      cc.addFcSubComponentNotification(cmp);
    } catch (IllegalContentException e) {
      throw new InstantiationException("Factory can't add new component as a sub component" + e.getMessage());
    } catch (IllegalLifeCycleException e) {
      throw new InstantiationException("Factory can't add new component as a sub component" + e.getMessage());
    }
    return cmp;
  }
}
